package com.xsdl.singleton;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/*
 * 线程内唯一的单例模式
 */
public class MIDGenerator {

    private final AtomicLong id = new AtomicLong(1L);

    private static final Map<Long, MIDGenerator> instances = new ConcurrentHashMap<>();

    private static final MIDGenerator instance = new MIDGenerator();

    private MIDGenerator() {

    }

    public static MIDGenerator getInstance() {
        long threadId = Thread.currentThread().getId();
        instances.putIfAbsent(threadId, new MIDGenerator());
        return instances.get(threadId);
    }

    public Long getNextId() {
        return id.getAndIncrement();
    }

}
